package xuegao.study.mall.admin.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.admin.dto.PmsProductCategoryParam;
import xuegao.study.mall.admin.service.pms.PmsProductCategoryService;
import xuegao.study.mall.common.api.CommonPage;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.model.pms.PmsProductCategory;
import xuegao.study.mall.common.vo.pms.PmsProductCategoryVo;

import java.util.List;

@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {

    @Autowired
    private PmsProductCategoryService pmsProductCategoryService;

    @ApiOperation("分页查询商品分类")
    @GetMapping("/list/{parentId}")
    public CommonResult<CommonPage<PmsProductCategory>> list(@PathVariable("parentId") Long parentId,
                                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        List<PmsProductCategory> list = pmsProductCategoryService.list(parentId, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("查询所有一级分类及子分类")
    @GetMapping("/list/withChildren")
    public CommonResult<List<PmsProductCategoryVo>> listWithChildren() {
        List<PmsProductCategoryVo> list = pmsProductCategoryService.listWithChildren();
        return CommonResult.success(list);
    }

    @ApiOperation("商品分类详情")
    @GetMapping("{id}")
    public CommonResult<PmsProductCategory> detail(@PathVariable("id") Long id) {
        PmsProductCategory item = pmsProductCategoryService.getItem(id);
        return CommonResult.success(item);
    }

    @ApiOperation("添加商品分类")
    @PostMapping(value = "/create")
    public CommonResult create(@RequestBody PmsProductCategoryParam param) {
        int count = pmsProductCategoryService.create(param);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改商品分类")
    @PostMapping(value = "/update/{id}")
    public CommonResult update(@PathVariable Long id, @RequestBody PmsProductCategoryParam param) {
        int count = pmsProductCategoryService.update(id, param);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}
